
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>2.1.2 Python实现 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../22-fu-wu-tong-xin.html" />
    
    
    <link rel="prev" href="211cshi-xian.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../../chapter1.html">
            
                <a href="../../chapter1.html">
            
                    
                    第1章 Cyber RT 概述与环境搭建
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../../chapter1/11-cyber-rt-jian-jie.html">
            
                <a href="../../chapter1/11-cyber-rt-jian-jie.html">
            
                    
                    1.1 Cyber RT 简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../../chapter1/12-cyber-rt-an-zhuang.html">
            
                <a href="../../chapter1/12-cyber-rt-an-zhuang.html">
            
                    
                    1.2 Cyber RT 安装
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.2.1" data-path="../../chapter1/12-cyber-rt-an-zhuang/121-zhun-bei-gong-zuo.html">
            
                <a href="../../chapter1/12-cyber-rt-an-zhuang/121-zhun-bei-gong-zuo.html">
            
                    
                    1.2.1 准备工作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.2" data-path="../../chapter1/12-cyber-rt-an-zhuang/122-cyber-rt-an-zhuang.html">
            
                <a href="../../chapter1/12-cyber-rt-an-zhuang/122-cyber-rt-an-zhuang.html">
            
                    
                    1.2.2 Cyber RT 安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.3" data-path="../../chapter1/12-cyber-rt-an-zhuang/123-ce-shi.html">
            
                <a href="../../chapter1/12-cyber-rt-an-zhuang/123-ce-shi.html">
            
                    
                    1.2.3 测试
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../../chapter1/14-cyber-rt-ji-cheng-kai-fa-huan-jing-da-jian.html">
            
                <a href="../../chapter1/14-cyber-rt-ji-cheng-kai-fa-huan-jing-da-jian.html">
            
                    
                    1.3 Cyber RT 集成开发环境搭建
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.3.1" data-path="../../chapter1/14-cyber-rt-ji-cheng-kai-fa-huan-jing-da-jian/131-vscode-xia-zai-yu-an-zhuang.html">
            
                <a href="../../chapter1/14-cyber-rt-ji-cheng-kai-fa-huan-jing-da-jian/131-vscode-xia-zai-yu-an-zhuang.html">
            
                    
                    1.3.1 Vscode 下载与安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.2" data-path="../../chapter1/14-cyber-rt-ji-cheng-kai-fa-huan-jing-da-jian/132-vscode-cha-jian-de-an-zhuang-yi-ji-shi-yong.html">
            
                <a href="../../chapter1/14-cyber-rt-ji-cheng-kai-fa-huan-jing-da-jian/132-vscode-cha-jian-de-an-zhuang-yi-ji-shi-yong.html">
            
                    
                    1.3.2 Vscode 插件的安装以及使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../../chapter1/13-cyber-rt-kuai-su-ti-yan.html">
            
                <a href="../../chapter1/13-cyber-rt-kuai-su-ti-yan.html">
            
                    
                    1.4 Cyber RT 快速体验
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.4.1" data-path="../../chapter1/13-cyber-rt-kuai-su-ti-yan/141-cshi-xian-helloworld.html">
            
                <a href="../../chapter1/13-cyber-rt-kuai-su-ti-yan/141-cshi-xian-helloworld.html">
            
                    
                    1.4.1 c++实现HelloWorld
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.2" data-path="../../chapter1/13-cyber-rt-kuai-su-ti-yan/142-pythonshi-xian-helloworld.html">
            
                <a href="../../chapter1/13-cyber-rt-kuai-su-ti-yan/142-pythonshi-xian-helloworld.html">
            
                    
                    1.4.2 Python实现HelloWorld
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../../chapter1/15-zhun-bei-1-bazel.html">
            
                <a href="../../chapter1/15-zhun-bei-1-bazel.html">
            
                    
                    1.5 预备知识——bazel
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.5.1" data-path="../../chapter1/15-zhun-bei-1-bazel/151-bazel-ji-ben-shi-yong-liu-cheng.html">
            
                <a href="../../chapter1/15-zhun-bei-1-bazel/151-bazel-ji-ben-shi-yong-liu-cheng.html">
            
                    
                    1.5.1 Bazel使用之基本流程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5.2" data-path="../../chapter1/15-zhun-bei-1-bazel/151-bazel.html">
            
                <a href="../../chapter1/15-zhun-bei-1-bazel/151-bazel.html">
            
                    
                    1.5.2 Bazel使用之库依赖
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../../chapter1/16-ben-zhang-xiao-jie.html">
            
                <a href="../../chapter1/16-ben-zhang-xiao-jie.html">
            
                    
                    1.6 预备知识——protobuf
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.6.1" data-path="../../chapter1/16-ben-zhang-xiao-jie/161-protoshi-yong-zhi-wen-jian-chuang-jian.html">
            
                <a href="../../chapter1/16-ben-zhang-xiao-jie/161-protoshi-yong-zhi-wen-jian-chuang-jian.html">
            
                    
                    1.6.1 proto使用之文件创建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6.2" data-path="../../chapter1/16-ben-zhang-xiao-jie/162-protoshi-yong-zhi-du-xie-cao-zuo.html">
            
                <a href="../../chapter1/16-ben-zhang-xiao-jie/162-protoshi-yong-zhi-du-xie-cao-zuo.html">
            
                    
                    1.6.2 proto读写之C++实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6.3" data-path="../../chapter1/16-ben-zhang-xiao-jie/163-protodu-xie-zhi-python-shi-xian.html">
            
                <a href="../../chapter1/16-ben-zhang-xiao-jie/163-protodu-xie-zhi-python-shi-xian.html">
            
                    
                    1.6.3 proto读写之Python实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../../chapter1/17-ben-zhang-xiao-jie.html">
            
                <a href="../../chapter1/17-ben-zhang-xiao-jie.html">
            
                    
                    1.7 本章小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../../di-2-zhang-cyber-rt-tong-xin-ji-zhi.html">
            
                <a href="../../di-2-zhang-cyber-rt-tong-xin-ji-zhi.html">
            
                    
                    第2章 Cyber RT 通信机制
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../21-hua-ti-tong-xin.html">
            
                <a href="../21-hua-ti-tong-xin.html">
            
                    
                    2.1 话题通信
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1.1" data-path="211cshi-xian.html">
            
                <a href="211cshi-xian.html">
            
                    
                    2.1.1C++实现
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.1.2" data-path="212-pythonshi-xian.html">
            
                <a href="212-pythonshi-xian.html">
            
                    
                    2.1.2 Python实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../22-fu-wu-tong-xin.html">
            
                <a href="../22-fu-wu-tong-xin.html">
            
                    
                    2.2 服务通信
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.2.1" data-path="../22-fu-wu-tong-xin/221-cshi-xian.html">
            
                <a href="../22-fu-wu-tong-xin/221-cshi-xian.html">
            
                    
                    2.2.1 C++实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.2" data-path="../22-fu-wu-tong-xin/222-pythonshi-xian.html">
            
                <a href="../22-fu-wu-tong-xin/222-pythonshi-xian.html">
            
                    
                    2.2.2 Python实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../23-can-shu.html">
            
                <a href="../23-can-shu.html">
            
                    
                    2.3 参数
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.3.1" data-path="../23-can-shu/231-cshi-xian.html">
            
                <a href="../23-can-shu/231-cshi-xian.html">
            
                    
                    2.3.1 C++实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3.2" data-path="../23-can-shu/232-pythonshi-xian.html">
            
                <a href="../23-can-shu/232-pythonshi-xian.html">
            
                    
                    2.3.2 Python实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../24-zu-jian.html">
            
                <a href="../24-zu-jian.html">
            
                    
                    2.4 组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.4.1" data-path="../24-zu-jian/241-ji-ben-component-yan-shi.html">
            
                <a href="../24-zu-jian/241-ji-ben-component-yan-shi.html">
            
                    
                    2.4.1 通用组件演示(上)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4.2" data-path="../24-zu-jian/242-ji-ben-zu-jian-yan-793a28-4e0b29.html">
            
                <a href="../24-zu-jian/242-ji-ben-zu-jian-yan-793a28-4e0b29.html">
            
                    
                    2.4.2 通用组件演示(下)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4.3" data-path="../24-zu-jian/242-shi-jian-zu-jian-yan-shi.html">
            
                <a href="../24-zu-jian/242-shi-jian-zu-jian-yan-shi.html">
            
                    
                    2.4.3 定时器组件演示
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../25-ben-zhang-xiao-jie.html">
            
                <a href="../25-ben-zhang-xiao-jie.html">
            
                    
                    2.5 本章小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../../di-3-zhang-cyber-rt-kai-fa-gong-ju.html">
            
                <a href="../../di-3-zhang-cyber-rt-kai-fa-gong-ju.html">
            
                    
                    第3章 Cyber RT 开发工具
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../../di-3-zhang-cyber-rt-kai-fa-gong-ju/32-cyberrecorder.html">
            
                <a href="../../di-3-zhang-cyber-rt-kai-fa-gong-ju/32-cyberrecorder.html">
            
                    
                    3.1 cyber record
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1.1" data-path="../../di-3-zhang-cyber-rt-kai-fa-gong-ju/32-cyberrecorder/321-c++-shi-xian.html">
            
                <a href="../../di-3-zhang-cyber-rt-kai-fa-gong-ju/32-cyberrecorder/321-c++-shi-xian.html">
            
                    
                    3.2.1 C++ 实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.1.2" data-path="../../di-3-zhang-cyber-rt-kai-fa-gong-ju/32-cyberrecorder/332-python-shi-xian.html">
            
                <a href="../../di-3-zhang-cyber-rt-kai-fa-gong-ju/32-cyberrecorder/332-python-shi-xian.html">
            
                    
                    3.3.2 Python 实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../25-chang-yong-ming-ling.html">
            
                <a href="../25-chang-yong-ming-ling.html">
            
                    
                    3.2 常用命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../../di-3-zhang-cyber-rt-kai-fa-gong-ju/33-ben-zhang-xiao-jie.html">
            
                <a href="../../di-3-zhang-cyber-rt-kai-fa-gong-ju/33-ben-zhang-xiao-jie.html">
            
                    
                    3.3 本章小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../../zi-65993a-chang-yong-api.html">
            
                <a href="../../zi-65993a-chang-yong-api.html">
            
                    
                    第4章 Cyber RT 常用API
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../../zi-65993a-chang-yong-api/1apiwen-dang.html">
            
                <a href="../../zi-65993a-chang-yong-api/1apiwen-dang.html">
            
                    
                    4.1 节点相关API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../../zi-65993a-chang-yong-api/2shi-jian-xiang-guan-api.html">
            
                <a href="../../zi-65993a-chang-yong-api/2shi-jian-xiang-guan-api.html">
            
                    
                    4.2 话题相关API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../../zi-65993a-chang-yong-api/3ding-shi-qi-xiang-guan-api.html">
            
                <a href="../../zi-65993a-chang-yong-api/3ding-shi-qi-xiang-guan-api.html">
            
                    
                    4.3 服务相关API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../../zi-65993a-chang-yong-api/44-can-shu-xiang-guan-api.html">
            
                <a href="../../zi-65993a-chang-yong-api/44-can-shu-xiang-guan-api.html">
            
                    
                    4.4 参数相关API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../../zi-65993a-chang-yong-api/45-zu-jian-xiang-guan-api.html">
            
                <a href="../../zi-65993a-chang-yong-api/45-zu-jian-xiang-guan-api.html">
            
                    
                    4.5 组件相关API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../../zi-65993a-chang-yong-api/46-shi-jian-xiang-guan-api.html">
            
                <a href="../../zi-65993a-chang-yong-api/46-shi-jian-xiang-guan-api.html">
            
                    
                    4.6 时间相关API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="../../zi-65993a-chang-yong-api/47-ri-zhi-xiang-guan-api.html">
            
                <a href="../../zi-65993a-chang-yong-api/47-ri-zhi-xiang-guan-api.html">
            
                    
                    4.7 日志相关API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="../../zi-65993a-chang-yong-api/48-record-xiang-guan-api.html">
            
                <a href="../../zi-65993a-chang-yong-api/48-record-xiang-guan-api.html">
            
                    
                    4.8 record 相关API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9" data-path="../../zi-65993a-chang-yong-api/48-ben-zhang-xiao-jie.html">
            
                <a href="../../zi-65993a-chang-yong-api/48-ben-zhang-xiao-jie.html">
            
                    
                    4.9 本章小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >2.1.2 Python实现</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h3 id="212-python&#x5B9E;&#x73B0;">2.1.2 Python&#x5B9E;&#x73B0;</h3>
<h4 id="1&#x53D1;&#x5E03;&#x65B9;&#x5B9E;&#x73B0;">1.&#x53D1;&#x5E03;&#x65B9;&#x5B9E;&#x73B0;</h4>
<p>demo_py &#x76EE;&#x5F55;&#x4E0B;&#x65B0;&#x5EFA; Python &#x6587;&#x4EF6; demo01_talker_py.py&#xFF0C;&#x8F93;&#x5165;&#x5982;&#x4E0B;&#x5185;&#x5BB9;&#xFF1A;</p>
<pre><code class="lang-py"><span class="hljs-comment">#!/usr/bin/env python3</span>

<span class="hljs-string">&apos;&apos;&apos;  
    &#x9700;&#x6C42;:&#x5B9E;&#x73B0;&#x6D88;&#x606F;&#x53D1;&#x5E03;&#x65B9;&#xFF0C;&#x53D1;&#x5E03;&#x6570;&#x636E;

    &#x5B9E;&#x73B0;:
        1.&#x5BFC;&#x5305;;
        2.&#x521D;&#x59CB;&#x5316;cyber&#x6846;&#x67B6;;
        3.&#x521B;&#x5EFA;&#x8282;&#x70B9;&#xFF1B;
        4.&#x521B;&#x5EFA;&#x53D1;&#x5E03;&#x65B9;&#xFF1B;
        5.&#x7EC4;&#x7EC7;&#x5E76;&#x53D1;&#x9001;&#x6570;&#x636E;&#xFF1B;
        6.&#x7B49;&#x5F85;&#x5173;&#x95ED;&#x3002;

&apos;&apos;&apos;</span>
<span class="hljs-keyword">from</span> cyber.python.cyber_py3 <span class="hljs-keyword">import</span> cyber
<span class="hljs-keyword">from</span> cyber.demo_base_proto.student_pb2 <span class="hljs-keyword">import</span> Student
<span class="hljs-keyword">import</span> time
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">talk</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-comment"># 3.&#x521B;&#x5EFA;&#x8282;&#x70B9;&#xFF1B;</span>
    talker_node = cyber.Node(<span class="hljs-string">&quot;ergou_py&quot;</span>)
    <span class="hljs-comment"># 4.&#x521B;&#x5EFA;&#x53D1;&#x5E03;&#x65B9;&#xFF1B;</span>
    talker = talker_node.create_writer(<span class="hljs-string">&quot;chatter&quot;</span>,Student)
    <span class="hljs-comment"># 5.&#x7EC4;&#x7EC7;&#x5E76;&#x53D1;&#x9001;&#x6570;&#x636E;&#xFF1B;</span>
    seq = <span class="hljs-number">0</span>
    <span class="hljs-keyword">while</span> <span class="hljs-keyword">not</span> cyber.is_shutdown():
        seq += <span class="hljs-number">1</span>
        print(<span class="hljs-string">&quot;send message %d&quot;</span> % seq)
        stu = Student()
        stu.name = <span class="hljs-string">&quot;huluwa&quot;</span>
        stu.age = seq
        stu.height = <span class="hljs-number">1.78</span>
        stu.books.append(<span class="hljs-string">&quot;yuwen&quot;</span>)
        stu.books.append(<span class="hljs-string">&quot;shuxue&quot;</span>)
        stu.books.append(<span class="hljs-string">&quot;yingyu&quot;</span>)


        talker.write(stu)
        time.sleep(<span class="hljs-number">1.0</span>)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
    cyber.init()
    <span class="hljs-comment">#&#x4E3B;&#x903B;&#x8F91;&#x5C01;&#x88C5;&#x8FDB;&#x51FD;&#x6570;</span>
    talk()
    cyber.shutdown()
</code></pre>
<p>BUILD&#x6587;&#x4EF6;:</p>
<pre><code>py_binary(
    name = &quot;demo01_talker_py&quot;,
    srcs = [&quot;demo01_talker_py.py&quot;],
    deps = [
        &quot;//cyber/python/cyber_py3:cyber&quot;,
        &quot;//cyber/demo_base_proto:student_py&quot;
    ] 

)
</code></pre><h4 id="2&#x8BA2;&#x9605;&#x65B9;&#x5B9E;&#x73B0;">2.&#x8BA2;&#x9605;&#x65B9;&#x5B9E;&#x73B0;</h4>
<p>demo_py &#x76EE;&#x5F55;&#x4E0B;&#x65B0;&#x5EFA; Python &#x6587;&#x4EF6; demo02_listener_py.py&#xFF0C;&#x8F93;&#x5165;&#x5982;&#x4E0B;&#x5185;&#x5BB9;&#xFF1A;</p>
<pre><code class="lang-py"><span class="hljs-comment">#!/usr/bin/env python3</span>
<span class="hljs-string">&apos;&apos;&apos;  
    &#x9700;&#x6C42;:&#x8BA2;&#x9605;&#x6570;&#x636E;
    &#x5B9E;&#x73B0;:
        1.&#x5BFC;&#x5305;&#xFF1B;
        2.&#x521D;&#x59CB;&#x5316; cyber &#x6846;&#x67B6;&#xFF1B;
        3.&#x521B;&#x5EFA;&#x8BA2;&#x9605;&#x8282;&#x70B9;&#xFF1B;
        4.&#x521B;&#x5EFA;&#x8BA2;&#x9605;&#x65B9;&#xFF1B;
        5.&#x5904;&#x7406;&#x6570;&#x636E;&#xFF1B;
        6.&#x7B49;&#x5F85;&#x5173;&#x95ED;&#x3002;

&apos;&apos;&apos;</span>
<span class="hljs-keyword">from</span> cyber.python.cyber_py3 <span class="hljs-keyword">import</span> cyber
<span class="hljs-keyword">from</span> cyber.demo_base_proto.student_pb2 <span class="hljs-keyword">import</span> Student

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">cb</span><span class="hljs-params">(stu)</span>:</span>
    <span class="hljs-comment"># print(stu)</span>
    print(<span class="hljs-string">&quot;name = %s age = %d height = %.2f&quot;</span> % (stu.name, stu.age, stu.height))
    <span class="hljs-keyword">for</span> book <span class="hljs-keyword">in</span> stu.books:
        print(<span class="hljs-string">&quot;book: %s&quot;</span> %book)
    print(<span class="hljs-string">&quot;-&quot;</span> * <span class="hljs-number">80</span>)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">listen</span><span class="hljs-params">()</span>:</span>
    listener_node = cyber.Node(<span class="hljs-string">&quot;cuihua_py&quot;</span>)
    listener = listener_node.create_reader(<span class="hljs-string">&quot;chatter&quot;</span>,Student,cb)
    listener_node.spin()

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
    cyber.init()
    listen()
    cyber.shutdown()
</code></pre>
<p>BUILD&#x6587;&#x4EF6;:</p>
<pre><code>py_binary(
    name = &quot;demo02_listener_py&quot;,
    srcs = [&quot;demo02_listener_py.py&quot;],
    deps = [
        &quot;//cyber/python/cyber_py3:cyber&quot;,
        &quot;//cyber/demo_base_proto:student_py&quot;
    ] 

)
</code></pre><h4 id="3&#x7F16;&#x8BD1;&#x6267;&#x884C;">3.&#x7F16;&#x8BD1;&#x6267;&#x884C;</h4>
<p>&#x7F16;&#x8BD1;&#xFF1A;</p>
<pre><code>bazel build cyber/demo_py/...
</code></pre><p>&#x6267;&#x884C;(&#x9700;&#x8981;&#x4E24;&#x4E2A;&#x7EC8;&#x7AEF;)&#xFF1A;</p>
<p>&#x7EC8;&#x7AEF;A&#x6267;&#x884C;&#x53D1;&#x5E03;&#x65B9;&#xFF1A;</p>
<pre><code>source cyber/setup.bash
</code></pre><pre><code>./bazel-bin/cyber/demo_py/demo01_talker_py
</code></pre><p>&#x7EC8;&#x7AEF;B&#x6267;&#x884C;&#x8BA2;&#x9605;&#x65B9;&#xFF1A;</p>
<pre><code>source cyber/setup.bash
</code></pre><pre><code>./bazel-bin/cyber/demo_py/demo02_listener_py
</code></pre><p>&#x6700;&#x7EC8;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x4E0E;&#x672C;&#x7AE0;&#x5F15;&#x8A00;&#x90E8;&#x5206;&#x7684;&#x6848;&#x4F8B;1&#x7C7B;&#x4F3C;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="211cshi-xian.html" class="navigation navigation-prev " aria-label="Previous page: 2.1.1C++实现">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../22-fu-wu-tong-xin.html" class="navigation navigation-next " aria-label="Next page: 2.2 服务通信">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"2.1.2 Python实现","level":"1.3.1.2","depth":3,"next":{"title":"2.2 服务通信","level":"1.3.2","depth":2,"path":"di-2-zhang-cyber-rt-tong-xin-ji-zhi/22-fu-wu-tong-xin.md","ref":"di-2-zhang-cyber-rt-tong-xin-ji-zhi/22-fu-wu-tong-xin.md","articles":[{"title":"2.2.1 C++实现","level":"1.3.2.1","depth":3,"path":"di-2-zhang-cyber-rt-tong-xin-ji-zhi/22-fu-wu-tong-xin/221-cshi-xian.md","ref":"di-2-zhang-cyber-rt-tong-xin-ji-zhi/22-fu-wu-tong-xin/221-cshi-xian.md","articles":[]},{"title":"2.2.2 Python实现","level":"1.3.2.2","depth":3,"path":"di-2-zhang-cyber-rt-tong-xin-ji-zhi/22-fu-wu-tong-xin/222-pythonshi-xian.md","ref":"di-2-zhang-cyber-rt-tong-xin-ji-zhi/22-fu-wu-tong-xin/222-pythonshi-xian.md","articles":[]}]},"previous":{"title":"2.1.1C++实现","level":"1.3.1.1","depth":3,"path":"di-2-zhang-cyber-rt-tong-xin-ji-zhi/21-hua-ti-tong-xin/211cshi-xian.md","ref":"di-2-zhang-cyber-rt-tong-xin-ji-zhi/21-hua-ti-tong-xin/211cshi-xian.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["expandable-chapters"],"pluginsConfig":{"expandable-chapters":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"di-2-zhang-cyber-rt-tong-xin-ji-zhi/21-hua-ti-tong-xin/212-pythonshi-xian.md","mtime":"2022-03-30T09:53:49.090Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-04-10T10:28:55.066Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

